﻿Ext.extend(demo.module,{
    init: function(){  
 	  var opt={ 
	  url:'faqquery'	  
	  };
	  
	  var display={		
		   xtype:'combo',
			  store: new Ext.data.SimpleStore({
			  fields: ['v','t'],data: [['0','yes'],['1','no']]}					
			),
		   fieldLabel:'<font color=red>前台显示</font>',
		   name:'display',		   
		   valueField:'v',							
		   displayField: 't',							
		   typeAhead: true,
		   value:"1",				          
		   mode: 'local',
		   readOnly:true,
		   triggerAction: 'all',
		   forceSelection: true,
		   selectOnFocus:true
	  }; 	  
	  var pageParams={start:0,limit:20};
	  opt.fields=[
	  {name:'id',text:'id',header:{hidden:true},ft:'hidden'},
	  {name:'isnew',text:'isnew',header:{hidden:true}},
	  {name:'question',text:'问题',header:{renderer:function(v,m,r){
	     if(r.get("isnew")==0){return "<font color=red>NEW</font>&nbsp;&nbsp;"+v;}else{return v}
	  }}},
	  {name:'display',text:'显示',header:{width:30,renderer:function(v){return v=='0'?'yes':'no';}}},
	  {name:'seq',text:'顺序',ft:{xtype:'numberfield',allowBlank:false,allowNegative:false,allowDecimals:false}},
	  {name:'addtime',text:'询问时间',header:{width:60,renderer:function(v){return Ext.displayTime12(v);}}},
	  {name:'asker',text:'问者',header:{width:50}},	  
	  {name:'detail',text:'问题描述'},	 
	  {name:'answer',text:'回答'}
	  ];
	  opt.formConfig={};
	  opt.formConfig.items=[
	   {
	     title:'提问',
	     layout:'form',
	     autoHeight:true,
	     autoWidth:true,
	     xtype:'fieldset',
	     items:[{xtype:'hidden',name:'id'}
	      ,display
	      ,{xtype:'textfield',name:'asker',fieldLabel:'问者',anchor:'96%'}
	      ,{xtype:'datefield',name:'addtime',fieldLabel:'询问时间',format:Ext.timeSaveFormat,menu:new DatetimeMenu(),anchor:'96%'}
	      ,{xtype:'textfield',name:'question',fieldLabel:'问题',anchor:'96%'}
	      ,{xtype:'textarea',name:'detail',height:80,fieldLabel:'问题描述',anchor:'96%'}	
	     ]
	   },
	   {
	     title:'解答',
	     layout:'form',
	     autoHeight:true,
	     autoWidth:true,
	     xtype:'fieldset',
	     items:[
	      {xtype:'numberfield',name:'seq',fieldLabel:'顺序'},
	      {xtype:'textarea',name:'answer',fieldLabel:'回答',height:80,anchor:'96%'}
	     ]	     
	   }
	  ];
	  opt.windowConfig={width:500};	  
	  opt.gridConfig={};	
	  opt.gridConfig.tbar=[
	   {text:'添加',iconCls:'add',handler:function(x){
	     this.window.setTitle(x);
	     this.window.show();
	   }},
	   {
	     text:'修改',iconCls:'edit',handler:function(x)
	     {
	       this.window.setTitle(x);
	       this.edit();	 
	     }
	   },
	   {
	     text:'删除',iconCls:'delete',handler:function(x)
	     {	       
	       this.del(function(rs){
	         this.postJson({
	           url:'deletefaq',
	           params:{faq:{id:rs[0].get("id")}}
	         })
	       })
	     }	   
	   },
	   '->',
	   '-',
	   {
		   xtype:'searchfield',		   
		   paramName:'faq.question',
		   defaultParams:pageParams,		  
		   emptyText:'search question',
		   listeners:{
		    usestore:function(){
		      this.store=g.ds;
		    }
		   }
		}	   	   
	  ];  
     
     opt.gridConfig.bbar={pageSize:pageParams.limit};     
	 var g=new Ext.fay.ui.CURDGrid(opt);
	 g.on("save",function(values){
	    var display=this.form.getForm().findField("display").getValue();
	    values.display=display;	 	    
	    if(this.isValid())
	    {
	      this.postJson({
	       url:'savefaq',
	       params:{faq:values}
	      })
	    }
	 });  
	 this.main.add(g.grid);
     this.main.doLayout();   
     g.ds.reload({params:pageParams});
   }
});